package com.wontube.smr.bo.pub;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public final class ProxyHttpBO {
	private ProxyHttpBO() {
	}

	public static final String fectchHtml(String url, Map<String, String> requestProperties) throws IOException {
		StringBuilder sb = new StringBuilder();

		URL uri = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
		conn.setRequestMethod("GET");
		for (Map.Entry<String, String> entry : requestProperties.entrySet()) {
			conn.setRequestProperty(entry.getKey(), entry.getValue());
		}
		InputStreamReader isr = new InputStreamReader(conn.getInputStream());
		try {
			char[] buffer = new char[1024];
			while (true) {
				int len = isr.read(buffer, 0, buffer.length);
				if (len <= 0) {
					break;
				}
				sb.append(buffer, 0, len);
			}
		} finally {
			isr.close();
			conn.disconnect();
		}

		return sb.toString();
	}
}
